<?php


namespace app\community\oauth2\Repository;


use app\community\oauth2\Entities\AuthCodeEntity;
use app\community\oauth2\Model\oauthUserAccessCodeModel;
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;

class AuthCodeRepository implements AuthCodeRepositoryInterface
{
    public function getNewAuthCode(): AuthCodeEntityInterface
    {
        // TODO: Implement getNewAuthCode() method.
        return app(AuthCodeEntity::class);
    }

    public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
    {
        // TODO: Implement persistNewAuthCode() method.
        // 此处将 code 唯一验证码 写入数据库
        $authCodeEntity -> setRedirectUri(
            $authCodeEntity -> getClient() -> getRedirectUri()
        );
        return (new oauthUserAccessCodeModel()) -> saveAccessCode($authCodeEntity);
    }

    public function revokeAuthCode($codeId)
    {
        // TODO: Implement revokeAuthCode() method.
        // TODO: 此处使用code后将code从数据库中销毁
        return (new oauthUserAccessCodeModel()) -> deleteData([
            'code' => $codeId,
            'app_id' => request() -> params('client_id')
        ]);
    }

    public function isAuthCodeRevoked($codeId)
    {
        // TODO: Implement isAuthCodeRevoked() method.
        // TODO: 此处需要查询数据库验证Code是否有效
        return (new oauthUserAccessCodeModel())
                -> hasAccessCode($codeId, request() -> params('client_id'), request() -> params('open_id'));
    }
}